package com.tuzhi.sys.web.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tuzhi.base.web.helper.AjaxResultHelper;
import com.tuzhi.base.web.vo.AjaxResultVO;
import com.tuzhi.common.constants.ResultConstant;
import com.tuzhi.common.domain.LoginUserBO;
import com.tuzhi.common.domain.ResultBO;
import com.tuzhi.common.helper.ResultHelper;
import com.tuzhi.sys.scurity.AuthTokenService;
import com.tuzhi.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.tuzhi.common.constants.security.Constants.AUTHORIZE_TOKEN;
import static com.tuzhi.common.constants.security.Constants.USER_INFO;


@Slf4j
@RestController
@Api(tags = "系统校验")
@RequestMapping("/permission")
public class AuthCheckController {
    static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }


    @Autowired
    AuthTokenService authTokenService;

    @ApiOperation(value = "检查用户权限")
    @PostMapping("/getCurUser")
    public ResultBO<LoginUserBO> getCurUser(HttpServletRequest request) {
        String token = request.getHeader(AUTHORIZE_TOKEN);
        if (StringUtils.isNotBlank(token)) {
            LoginUserBO loginUser = authTokenService.getLoginUser(token);
            if (loginUser != null) {
                return ResultHelper.success().ofData(loginUser);
            }
        }
        return ResultHelper.error(ResultConstant.ERR_TOKEN_ERR.getCode(),ResultConstant.ERR_TOKEN_ERR.getMsg());
    }


}
